package com.core.springSecurity.provider;

import com.common.ErrorCodeEnum;
import com.common.exception.BusinessException;
import com.core.sms.service.SmsServer;
import com.core.springSecurity.authentication.PasswordAuthenticationToken;
import com.core.springSecurity.authentication.SmsCodeAuthenticationToken;
import com.core.springSecurity.authentication.WXAuthenticationToken;
import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;

public class WXAuthenticationProvider implements AuthenticationProvider {

    private UserDetailsService userDetailsService;


    @Override
    public Authentication authenticate(Authentication authentication) throws AuthenticationException {
        WXAuthenticationToken authenticationToken = (WXAuthenticationToken) authentication;

        String mobile = (String) authenticationToken.getPrincipal();

        checkOpenid(authentication);

        UserDetails user = userDetailsService.loadUserByUsername(mobile);

        // 此时鉴权成功后，应当重新 new 一个拥有鉴权的 authenticationResult 返回
//        SmsCodeAuthenticationToken authenticationResult = new SmsCodeAuthenticationToken(userDetails,
//                authenticationToken.getCredentials(), userDetails.getAuthorities());

//        authenticationResult.setDetails(authenticationToken.getDetails());

        Object principalToReturn = user;

        return this.createSuccessAuthentication(principalToReturn, authentication, user);
    }

    protected Authentication createSuccessAuthentication(Object principal, Authentication authentication,
                                                         UserDetails user) {
        PasswordAuthenticationToken result = new PasswordAuthenticationToken(principal,
                authentication.getCredentials(), user.getAuthorities());
        result.setDetails(authentication.getDetails());
        SecurityContextHolder.getContext().setAuthentication(result);
        return result;
    }

    private void checkOpenid(Authentication authentication) {
        String mobile = (String) authentication.getPrincipal();
        String mobileCode = (String) authentication.getCredentials();

//        if (userBaseInfoService.getByCellphoneOrOpenId(mobile)==null) {
//            throw new BusinessException(ErrorCodeEnum.MOBILE_CODE_VERIFY_FAILED);
//        }

    }

    @Override
    public boolean supports(Class<?> authentication) {
        // 判断 authentication 是不是 SmsCodeAuthenticationToken 的子类或子接口
        return WXAuthenticationToken.class.isAssignableFrom(authentication);
    }

    public void setUserDetailsService(UserDetailsService userDetailsService) {
        this.userDetailsService = userDetailsService;
    }
}
